package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Consulter;
import com.woniuxy.oldmanapi.service.ConsultService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsultController {
    @Autowired
    ConsultService consultService;

    //查询咨询者信息
    @GetMapping("/consult/selectConsulter")
    public ResponseResult<PageInfo<Consulter>> selectConsulter(int now,int size,String cname){
        ResponseResult<PageInfo<Consulter>> result = new ResponseResult<>();
        try {
            PageInfo<Consulter> pageInfo = consultService.selectConsulter(now, size, cname);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //更新咨询者信息
    @PostMapping("/consult/updateConsulter")
    public ResponseResult<Void> updateConsulter(@RequestBody Consulter consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            consultService.updateConsulter(consulter);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //删除咨询者信息
    @PostMapping("/consult/deleteConsulter")
    public ResponseResult<Void> deleteConsulter(@RequestBody Consulter consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            consultService.deleteConsulter(consulter);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //新增咨询者信息
    @PostMapping("/consult/insertConsulter")
    public ResponseResult<Void> insertConsulter(@RequestBody Consulter consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            consultService.insertConsulter(consulter);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
}
